<?php
declare (strict_types=1);

namespace app\api\controller;

use app\admin\model\ReceivingAddress;
use think\facade\Validate;
use think\Request;

class AddressController
{
    /**
     * 收货地址的添加接口
     */
    public function addressAdd()
    {
        //接值
        $param = input();

        //表单验证
        $validate = Validate::rule([
            'consignee_name' => 'require',
            'phone' => 'require',
            'user_id' => 'require',
            'province' => 'require',
            'city' => 'require',
            'area' => 'require',
            'detail_address' => 'require',
            'is_default' => 'require'
        ]);
        if (!$validate->check($param)) return json(['code' => 4003, 'msg' => $validate->getError(), 'data' => '']);

        //判断是否为默认地址
        if ($param['is_default'] == 0) {
            //非默认地址（直接添加）
            $addAddress = ReceivingAddress::create($param);
            if (!$addAddress) return json(['code' => 4005, 'msg' => '添加失败', 'data' => '']);
            return json(['code' => 200, 'msg' => '添加成功', 'data' => '']);
        }

        //为默认地址（并修改该用户的其他地址为非默认）
        //先修改
        $upAddressRes = ReceivingAddress::where('user_id', $param['user_id'])->update(['is_default' => 0]);
        //在添加
        $addAddressRes = ReceivingAddress::create($param);
        if (!$upAddressRes || !$addAddressRes) return json(['code' => 4005, 'msg' => '添加失败', 'data' => '']);

        return json(['code' => 200, 'msg' => '添加成功', 'data' => '']);
    }

    /**
     * 收货地址展示接口
     */
    public function addressList()
    {
        //1、接收用户id
        $uid = input('user_id');

        //2、验证数据
        if (empty($uid) || !isset($uid)) return json(['code' => 4003, 'msg' => '请先登录', 'data' => '']);

        //3、查询收货地址
        $addressList = ReceivingAddress::where('user_id', $uid)->select();
        if (!$addressList) return json(['code' => 4006, 'msg' => '查询失败', 'data' => '']);

        return json(['code' => 200, 'msg' => '查询成功', 'data' => '']);
    }

    /**
     * 收货地址删除接口
     */
    public function addressDel()
    {
        //1、接收收货地址的id
        $id = input('id');

        //2、验证数据
        if (empty($id) || !isset($id)) return json(['code' => 4003, 'msg' => '请选择要删除的收货地址', 'data' => '']);

        //执行删除地址
        $addressDelRes = ReceivingAddress::destroy($id);
        if (!$addressDelRes) return json(['code' => 4007, 'msg' => '删除失败', 'data' => '']);

        return json(['code' => 200, 'msg' => '删除成功', 'data' => '']);
    }

    /**
     * 收货地址修改接口
     */
    public function addressUp()
    {
        //1、接收数据
        $param = input();

        //2、表单验证
        $validate = Validate::rule([
            'id' => 'require',
            'consignee_name' => 'require',
            'phone' => 'require',
            'user_id' => 'require',
            'province' => 'require',
            'city' => 'require',
            'area' => 'require',
            'detail_address' => 'require',
            'is_default' => 'require'
        ]);
        if (!$validate->check($param)) return json(['code' => 4003, 'msg' => $validate->getError(), 'data' => '']);

        //执行修改
        $addressUp = ReceivingAddress::where('id', $param['id'])->update($param);
        if (!$addressUp) return json(['code' => 4008, 'msg' => '修改失败', 'data' => '']);

        return json(['code' => 200, 'msg' => '修改成功', 'data' => '']);
    }

}
